from selenium.webdriver.common.by import By
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains,Keys
driver=webdriver.Edge()
driver.get('http://10.59.9.12:7001/')
# 登录账号并进行添加商品加入购物车进行购买
driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
driver.find_element(By.NAME,'username').send_keys('13012558376')
driver.find_element(By.NAME,'password').send_keys('123456Aa')
time.sleep(5)

# 鼠标事件
login=driver.find_element(By.CLASS_NAME,'finish-btn')
ActionChains(driver).click(login).perform()

# 强制等待
time.sleep(3)
driver.find_element(By.LINK_TEXT,'首页').click()
# 隐式等待
driver.implicitly_wait(10)
dayin=driver.find_element(By.XPATH,'//input[@class="search-input" and @name="keywords"]')
time.sleep(3)
dayin.send_keys('宋牌')

print(dayin.location)
print(dayin.size)
print(dayin.get_attribute('class'))
# 键盘回车事件
dayin.send_keys(Keys.ENTER)
# 显式等待
xian=WebDriverWait(driver,2,0.5).until(EC.presence_of_element_located((By.XPATH,'//input[starts-with(@class,"search-input-btn")]')))
xian.click()
driver.find_element(By.XPATH,'//img[@src="/uploads/images/20250620/67e1bd127f10d17039e45464df4c460a.jpg"]').click()
time.sleep(3)
driver.find_element(By.ID,'add-cart').click()
driver.find_element(By.CSS_SELECTOR,'.nav-settcart').click()
time.sleep(2)
driver.find_element(By.NAME,'id[]').click()
# 滚动事件 js事件
driver.execute_script('window.scrollTo(0,500)')
time.sleep(2)
driver.find_element(By.CLASS_NAME,'ask-btn').click()
driver.find_element(By.ID,'address-834').click()
driver.find_element(By.CLASS_NAME,'ask-btn').click()
time.sleep(5)
# 切换窗口‘
driver.switch_to.window(driver.window_handles[-1])
# 刷新浏览器
driver.refresh()
